package com.library.verification.window;

import android.content.Context;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;


/**
 * Created by zhoulikai on 17/4/12.
 * 通用窗体
 */

public class CommonPopWindow implements IWindow {
    private final static String TAG = "CommonPopWindow";
    private Context context;
    private PopupWindow popupWindow;

    public CommonPopWindow(Context context) {
        this.context = context;
    }

    @Override
    public void windowCreate(int layoutId) {
        makePopWindow(layoutId);
    }

    @Override
    public void windowCreate(View contentView) {
        makePopWindow(contentView);
    }

    @Override
    public void windowDestroy() {

    }

    @Override
    public void windowShow(View view) {
        if (!checkWindowInit()) {
            return;
        }
        popupWindow.showAtLocation(view, Gravity.CENTER, 0, 0);
    }

    @Override
    public void windowDismiss() {
        if (!checkWindowInit()) {
            return;
        }
        popupWindow.dismiss();
    }

    private boolean checkWindowInit() {
        if (popupWindow == null) {
            Log.d(TAG, "window为被初始化，请使用windowCreate初始化window");
            return false;
        }
        return true;
    }

    private void makePopWindow(int layoutId) {
        if (context == null) {
            return;
        }

        View contentView = null;
        try {
            contentView = LayoutInflater.from(context).inflate(layoutId, null);
        } catch (Exception e) {

        }
        makePopWindow(contentView);
    }

    private void makePopWindow(View contentView) {


        if (contentView == null) {
            return;
        }
        popupWindow = new PopupWindow(contentView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
        popupWindow.setFocusable(true);
        popupWindow.setOutsideTouchable(true);
        popupWindow.setTouchable(true);
//        popupWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
//        popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
    }
}
